package com.dsiab.manageService.threads;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class PushThread implements Runnable{
    private String postId;
    @Override
    public void run() {
        pushPost(postId);
    }

    public void setPostId(String postId) {
        this.postId = postId;
    }

    public void pushPost(String postId){
        // 百度推送
        String url = "http://data.zz.baidu.com/urls?site=https://www.dsiab.com&token=QBgEe50bgf09Iw0q";
        String paramMap = "https://www.dsiab.com/post/" + postId;
        String baiduPushResult = HttpRequest.post(url)
                .header(Header.USER_AGENT, "www.dsiab.com")//头信息，多个头信息多次调用此方法即可
                .contentType("text/plain")//表单内容
                .body(paramMap)
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.info(baiduPushResult);
        // bing 推送
        String bingUrl = "https://www.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=cf7ac56f335b4a7fa97f8f77595cf520";
        Map bingParamMap = new HashMap<>();
        bingParamMap.put("siteUrl", "https://www.dsiab.com");
        List list  = new ArrayList<>();
        list.add("https://www.dsiab.com/post/"+postId);
        bingParamMap.put("urlList",list);
        log.info(bingParamMap.toString());
        String bingPushResult = HttpRequest.post(bingUrl)
                .header(Header.USER_AGENT, "www.dsiab.com")//头信息，多个头信息多次调用此方法即可
                .contentType("application/json")//表单内容
                .body(JSONUtil.toJsonStr(bingParamMap))
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.info(bingPushResult);
    };
}
